import { createSlice, createAsyncThunk, AsyncThunk, AsyncThunkAction } from '@reduxjs/toolkit'

interface InitialStateType {
  msg: string
  list: any[]
}

const initialState: InitialStateType = {
  msg: '',
  list: [],
}

const createData = () => {
  return new Promise<any>((resolve, reject) => {
    setTimeout(() => {
      resolve({
        code: 0,
        msg: 'success',
      })
    }, 500)
  })
}

export const fetchTest: any = createAsyncThunk('dashboard/fetchTest', async () => {
  const data = await createData()
  return data
})

const dashboardSlice = createSlice({
  name: 'dashboard',
  initialState,
  reducers: {
    set_list(state, action) {
      state.list = action.payload
    },
  },
  // 监听异步
  extraReducers(builder) {
    builder.addCase(fetchTest.pending, () => {})
    builder.addCase(fetchTest.fulfilled, (state, action) => {
      // state.total = action.payload.total
      // state.list = action.payload.list
      console.log('state', state.list)
    })
  },
})

export default dashboardSlice.reducer

export const { set_list } = dashboardSlice.actions
